home *** CD-ROM | disk | FTP | other *** search
Text File | 1996-05-21 | 10.6 KB | 389 lines | [TEXT/ttxt] |
- --<<<-
- -- Filename:
- -- mediaimp.sx
-
- -- Other Files Required:
- -- This file is not part of any specific example, but rather a useful class for
- -- importing media and storing the media into ObjectStore.
-
- -- Purpose:
- -- Provide a layer to the import export engine which enables easy use of ObjectStore.
-
- -- Specialized Classes:
- -- MediaImporter
-
- -- Instructions to User:
- -- Class MediaImporter provides a layer to the import export engine
- -- which enables easy use of ObjectStore. When you create a MediaImporter,
- -- you may specify a library container in which media is automatically stored.
- -- The target collection of the library container may be either an explicitly or
- -- implicitly keyed collection subclass. When you are done importing, you can invoke
- -- the MediaImporter saveMedia method to close the library container.
- -- For each import method, you specify a fileName and optional mediaKey. The
- -- import adds an entry into the ExplicitKeyedCollection using the filename as
- -- the key, unless the mediaKey is supplied. In this case, mediaKey is used as
- -- the key.
-
- -- PUBLIC PROTOCOL
- -- importObject self fileName key:mediaKey
- -- importDIB self fileName key:mediaKey
- -- importPICT self fileName key:mediaKey
- -- importRTF self fileName key:mediaKey
- -- importAIFF self fileName key:mediaKey
- -- importWave self fileName key:mediaKey
- -- importQuicktime self fileName key:mediaKey
- -- importRes self type id key:mediaKey
- -- save self name
-
- -- KEYWORD ARGUMENTS
- -- dir -- dir from which to import media.
- -- container -- library container for media
- -- extension -- extension to add
- -- mediaCategory -- @image, @sound, @movie
- -- inputMediaType -- @dib, @pict, etc.
- -- outputMediaType -- @bitmap, @player, etc.
-
- -- EXAMPLE CREATION
- -- An importer which imports from the StartDir:
- -- mi := new MediaImporter
-
- -- An importer which imports from a folder called "media", and stores media in
- -- library container c
- -- mi := new MediaImporter dir:(spawn theStartDir "media") container:c
-
- -- Author:
- -- Steve Mayer
-
- in module AutofinderMedia
-
- class MediaImporter ()
- class variables
- extensionMapping -- mapping of file extensions to import parameters.
- instance variables
- dir -- directory from which to import media.
- container -- library container for media
- addExtensions -- if true, file extensions will be added for specific media.
- smartExtensions -- if true, file type is inferred from extension.
- extension -- extension to add
- mediaCategory -- @image, @sound, @movie
- inputMediaType -- @dib, @pict, etc.
- outputMediaType -- @bitmap, @player, etc.
- bundle -- ResBundle instance, if we are importing Res files.
- convertToShapes -- If true, convert bitmaps to twodshapes.
- shrinkBitmaps -- If true, shrink wrap bitmaps.
- matteColor -- if defined, set bitmap's matte to this color.
- invisibleColor -- if defined, set bitmaps's invisible color to this color.
- class methods
- method init self #rest args ->
- (
- apply nextMethod self args
- local em := new KeyedLinkedList
- add em ".dib" #(@Image, @DIB, @Bitmap)
- add em ".bmp" #(@Image, @DIB, @Bitmap)
- add em ".pic" #(@Image, @PICT, @Bitmap)
- add em ".aif" #(@Sound, @AIFF, @Player)
- add em ".wav" #(@Sound, @Wave, @Player)
- add em ".avi" #(@Movie, @AVI, @Player)
- add em ".mov" #(@Movie, @Quicktime, @Player)
- add em ".rtf" #(@Text, @RTF, @RichText)
- self.extensionMapping := em
- self
- )
- end
-
- -- Method init saves keyword arguments in instance variables.
- method init self {class MediaImporter} #rest args #key \
- dir: (theStartDir) \
- container: (undefined) \
- addExtensions: (false) \
- smartExtensions: (false) \
- mediaCategory: (@image) \
- inputMediaType: (@dib) \
- outputMediaType: (@bitmap) \
- convertToShapes: (true) \
- shrinkBitmaps: (false) \
- mode: (@create) ->
- (
- apply nextMethod self args
- self.dir := dir
- self.container := container
- self.addExtensions := addExtensions
- self.smartExtensions := smartExtensions
- self.mediaCategory := mediaCategory
- self.inputMediaType := inputMediaType
- self.outputMediaType := outputMediaType
- self.matteColor := undefined
- self.invisibleColor := undefined
- self.convertToShapes := convertToShapes
- self.shrinkBitmaps := shrinkBitmaps
- return self
- )
-
- method getOne self {class MediaImporter} key ->
- (
- getOne self.container key
- )
-
- -- Method inferMediaType parses a filename to get its extension, and then
- -- calls setExtension on self.
- method inferMediaType self {class MediaImporter} fileName ->
- (
- -- Get extension part of filename.
- local ext := new String
- for i := ((size fileName) - 3) to (size fileName) do
- (
- append ext fileName[i]
- )
- setExtension self ext
- )
-
- method stripExtension self {class MediaImporter} fileName ->
- (
- -- Get prefix part of filename.
- local prefx := new String
- for i := 1 to ((size fileName) - 4) do
- (
- append prefx fileName[i]
- )
- return (prefx as StringConstant)
- )
-
- -- Method setExtension sets the current file extension to the specified
- -- value. It also automatically sets the import parameters based the
- -- MediaImporter.extensionMapping.
- method setExtension self {class MediaImporter} extension ->
- (
- self.extension := extension
- local result := MediaImporter.extensionMapping[extension]
- if (result <> empty) do
- (
- self.mediaCategory := result[1]
- self.inputMediaType := result[2]
- self.outputMediaType := result[3]
- )
- )
-
- -- Method shrinkBitmap "shrink-wraps" a bitmap.
- method shrinkBitmap self {class MediaImporter} bm ->
- (
- local box := bm.bbox
- local w := bm.bbox.width
- local h := bm.bbox.height
- local left := w, right := -1, top := h, bottom := -1
- local rowbytes := bm.rowbytes
- local data := bm.data
- local bg := data[1]
- local x := 0
- local y := 0
- foreach data (p xxx ->
- if ((p != bg) and (x < w)) do (
- if (x < left) do (left := x)
- if (x > right) do (right := x)
- if (y < top) do (top := y)
- if (y > bottom) do (bottom := y)
- )
- x := x + 1
- if (x == rowbytes) do (
- x := 0
- y := y + 1
- )
- ) ok
-
- bottom := bottom + 1
- right := right + 1
- if ((top = h) or (left = w) or (right = 0) or (bottom = 0)) do (
- top := 0
- left := 0
- right := 0
- bottom := 0
- )
- local box := new Rect x1:left y1:top x2:right y2:bottom
- print box
- local bms := new BitmapSurface colormap: bm.colormap bbox: box
- transfer bms (bm as BitmapSurface) bms (new TwoDMatrix)
- print #("Shrinking bitmap", #(w, h), "to", #(left, top, right, bottom),
- "saving", bm.data.size, bms.data.size, bm.data.size - bms.data.size)
-
- bms as Bitmap
- )
-
- -- Method processBitmap is called to process an imported bitmap, based on
- -- MediaImporter parameters.
- method processBitmap self {class MediaImporter} bm ->
- (
- if (self.shrinkBitmaps) do
- (
- bm := shrinkBitmap self bm
- )
- if (self.invisibleColor <> undefined) do
- (
- bm.invisibleColor := self.invisibleColor
- )
- if (self.matteColor <> undefined) do
- (
- bm.matteColor := self.matteColor
- )
- if (self.convertToShapes) do
- (
- bm := new TwoDShape boundary:bm fill:blackBrush
- )
- return bm
- )
-
- -- Method storeMedia handles adding the media to the library container
- method storeMedia self {class MediaImporter} name media ->
- (
- -- proxy for media if using a library container.
- if self.container <> undefined do
- (
- -- Add the media to the container.
- if (isAKindOf self.container.targetCollection ImplicitlyKeyedCollection) then
- (
- append self.container media
- ) else
- (
- add self.container name media
- )
- -- makePurgeable media
- )
- )
-
- method importObject self {class MediaImporter} fileName #key mediaKey: (undefined) \
- mediaStream:(undefined) ->
- (
- local mStream
- local mKey := fileName
- if (mediaStream <> undefined) then
- (
- mStream := mediaStream
- ) else
- (
- -- Create a media stream for the specified dir, and convert to output media type.
- if (self.addExtensions) do
- (
- fileName := fileName + self.extension
- )
-
- if (self.smartExtensions) do
- (
- -- Infer media type, and strip extension from filename.
- inferMediaType self fileName
- mKey := stripExtension self fileName
- )
- -- Build path for Media folder and append fileName.
- local p := copy (self.dir as Sequence)
- append p fileName
- mStream := getStream theRootDir p @Readable
- )
-
- -- This is no longer needed, right!??
- -- local storageCont := undefined
- -- if (self.container <> undefined) do
- -- (
- -- storageCont := self.container.storageContainer
- -- )
-
- local media := importMedia theImportExportEngine mStream \
- self.mediaCategory self.inputMediaType self.outputMediaType \
- container:self.container colorMap: defaultColorMap
-
- -- If a mediaKey is specified, use it.
- if (mediaKey <> undefined) do mKey := mediaKey
-
- -- If it is a bitmap, call the processBitmap method.
- if (self.mediaCategory = @Image) do
- (
- media := processBitmap self media
- )
-
- -- Store the media.
- storeMedia self mKey media
- -- What does this do?
- plug mStream
- return media
- )
-
- -- Method importAllMedia iterates through every file in the media importer's dir,
- -- and calls importObject.
- method importAllMedia self {class MediaImporter} ->
- (
- -- Iterate through all files in the dir.
- local failList := new Array
- local theErr := @all
- -- Iterate through all of the files.
- for fileName in (getContents self.dir) do
- (
- -- If it is a file (not a directory)...
- if (isFile self.dir fileName) do
- (
- guard
- (
- importObject self fileName
- )
- catching
- theErr : (
- append failList fileName
- caught theErr
- )
- on exit
- undefined
- end
- )
- )
- )
-
- -- The following methods support specific media types, and use the more
- -- general importObject method.
- method importPICT self {class MediaImporter} fileName #key mediaKey: ->
- (
- setExtension self ".pic"
- return importObject self fileName mediaKey:mediaKey
- )
-
- method importRTF self {class MediaImporter} fileName #key mediaKey: ->
- (
- setExtension self ".rtf"
- return importObject self fileName mediaKey:mediaKey
- )
-
- method importAIFF self {class MediaImporter} fileName #key mediaKey: ->
- (
- setExtension self ".aif"
- return importObject self fileName mediaKey:mediaKey
- )
-
- method importWave self {class MediaImporter} fileName #key mediaKey: ->
- (
- setExtension self ".wav"
- return importObject self fileName mediaKey:mediaKey
- )
-
- method importDIB self {class MediaImporter} fileName #key mediaKey: ->
- (
- setExtension self ".bmp"
- return importObject self fileName mediaKey:mediaKey
- )
-
- method importQuicktime self {class MediaImporter} fileName #key mediaKey: ->
- (
- setExtension self ".mov"
- return importObject self fileName mediaKey:mediaKey
- )
-
- method importRes self {class MediaImporter} type id #key mediaKey: (undefined) ->
- (
- setExtension self ".pic"
-
- -- If no media key is specified, use the res file id.
- local str := getOneStream self.bundle type:type ID:id
- return importObject self (id as String) mediaStream:str mediaKey:mediaKey
- )
-
- -- Close the container.
- method save self {class MediaImporter} ->
- (
- close self.container
- )
- "Compiled mediaimp.sx"
- -->>>
-